package org.linphone.Server;

import java.lang.reflect.Method;

import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;

import com.android.internal.telephony.ITelephony;

public class PhoneUtils {
	static String TAG = "PhoneUtils";

	/**
	 * 从TelephonyManager中实例化ITelephony,并返回
	 */
	static public ITelephony getITelephony(TelephonyManager telMgr)
			throws Exception {
		Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod(
				"getITelephony");
		getITelephonyMethod.setAccessible(true);// 私有化函数也能使用
		return (ITelephony) getITelephonyMethod.invoke(telMgr);
	}

	// 自动接听
	public synchronized void answerRingingCall2013()
    {//据说该方法只能用于Android2.3及2.3以上的版本上



		
			 
		
       
    }
	public static synchronized void autoAnswerPhone(Context c,
			TelephonyManager tm) {
		try
        {
		 
            Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
            localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            localIntent1.putExtra("state", 1);
            localIntent1.putExtra("microphone", 1);
            localIntent1.putExtra("name", "Headset");
            c.sendOrderedBroadcast(localIntent1, "android.permission.CALL_PRIVILEGED");
            Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);
            KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK);
            localIntent2.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent1);
            c.sendOrderedBroadcast(localIntent2, "android.permission.CALL_PRIVILEGED");
            Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);
            KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
            localIntent3.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent2);
            c.sendOrderedBroadcast(localIntent3, "android.permission.CALL_PRIVILEGED");
            Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);
            localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            localIntent4.putExtra("state", 0);
            localIntent4.putExtra("microphone", 1);
            localIntent4.putExtra("name", "Headset");
            c.sendOrderedBroadcast(localIntent4, "android.permission.CALL_PRIVILEGED");
            
        }
        catch (Exception e)
        {
            //e.printStackTrace();
        	//Show_Toast("提示5:" +"接听失败，请手动接听来电！");
        }

	 try
        {
	  Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);  
      KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);  
      meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent);  
      c.sendOrderedBroadcast(meidaButtonIntent, null);
      
        }
      catch (Exception e)
        {
            //e.printStackTrace();
        	//Show_Toast("提示5:" +"接听失败，请手动接听来电！");
        }
//		
	}

	// 自动挂断
	// public static synchronized void endPhone(Context c, TelephonyManager tm)
	// {
	// try {
	// Log.i(TAG, "endPhone");
	// ITelephony iTelephony;
	// Method getITelephonyMethod = TelephonyManager.class
	// .getDeclaredMethod("getITelephony", (Class[]) null);
	// getITelephonyMethod.setAccessible(true);
	// iTelephony = (ITelephony) getITelephonyMethod.invoke(tm,
	// (Object[]) null);
	// // 挂断电话
	// iTelephony.endCall();
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
}
